package com.cph.aop.controller;

import java.util.List;

import com.cph.aop.pojo.SysOperLog;
import com.cph.aop.service.ISysOperLogService;
import com.cph.common.util.SqlUtil;
import com.cph.common.util.StringUtils;
import com.cph.framework.page.PageDomain;
import com.cph.framework.page.TableSupport;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cph.common.http.HttpResponseResult;
import com.cph.common.http.HttpStatus;
import com.cph.framework.page.TableDataInfo;

/**
 * 操作日志记录Controller
 *
 * @author cph
 * @date 2020-02-27
 */
@RestController
@RequestMapping("/project/log")
public class SysOperLogController {
    @Autowired
    private ISysOperLogService sysOperLogService;

    /**
     * 查询操作日志记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysOperLog sysOperLog) {
        startPage();
        List<SysOperLog> list = sysOperLogService.selectSysOperLogList(sysOperLog);
        return getDataTable(list);
    }

    /**
     * 获取操作日志记录详细信息
     */
    @GetMapping(value = "/{operId}")
    public HttpResponseResult getInfo(@PathVariable("operId") Long operId) {
        return HttpResponseResult.success(sysOperLogService.selectSysOperLogById(operId));
    }

    /**
     * 新增操作日志记录
     */
    @PostMapping
    public HttpResponseResult add(@RequestBody SysOperLog sysOperLog) {
        return toHttpResponseResult(sysOperLogService.insertSysOperLog(sysOperLog));
    }

    /**
     * 修改操作日志记录
     */
    @PutMapping
    public HttpResponseResult edit(@RequestBody SysOperLog sysOperLog) {
        return toHttpResponseResult(sysOperLogService.updateSysOperLog(sysOperLog));
    }

    /**
     * 删除操作日志记录
     */
    @DeleteMapping("/{operIds}")
    public HttpResponseResult remove(@PathVariable Long[] operIds) {
        return toHttpResponseResult(sysOperLogService.deleteSysOperLogByIds(operIds));
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    /**
     * 设置请求分页数据
     */
    protected void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }
    /**
 * 响应返回结果
 *
 * @param rows 影响行数
 * @return 操作结果
 */
    protected HttpResponseResult toHttpResponseResult(int rows) {
        return rows > 0 ? HttpResponseResult.success() : HttpResponseResult.error();
    }
}
